package com.sise.cn.api.his.impl;

import com.sise.cn.api.common.impl.OrderServiceImpl;
import com.sise.cn.api.common.po.Order;
import com.sise.cn.api.enumerate.ServerResponseEnum;
import com.sise.cn.api.exception.OrderException;
import com.sise.cn.api.his.po.TicketOrderPO;
import com.sise.cn.api.his.service.TicketService;
import com.sise.cn.dao.base.BaseMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 龚秀峰
 * @version 1.0
 * @date 2020/6/11 22:35
 */
@Service
@Slf4j
public class TicketOrderServiceImpl extends OrderServiceImpl implements TicketService {

    @Resource
    private BaseMapper baseMapper;

    @Override
    public TicketOrderPO getOrderById(Long id) {
        TicketOrderPO ticket = new TicketOrderPO();
        ticket.setId(id);
        return baseMapper.select(ticket, TicketOrderPO.class);
    }

    @Override
    public Long saveOrder(Order order) {
        return baseMapper.insertAndGetId(order);
    }

    @Override
    public List<Order> getOrderByOrderCode(String orderCode) {
        TicketOrderPO ticket = new TicketOrderPO();
        ticket.setOrderCode(orderCode);
        List<Order> objects = baseMapper.selectList(ticket, TicketOrderPO.class);
        if (objects.size() > 1) {
            throw new OrderException(ServerResponseEnum.ORDER_REPEAT_ERROR);
        }
        return objects;
    }


    @Override
    public void updateOrderByOrderCode(Order order) {
        //检查是否重复、或者不存在
        final List<Order> orderList = getOrderByOrderCode(order.getOrderCode());
        if (orderList.isEmpty()) {
            throw new OrderException(ServerResponseEnum.ORDER_NOT_EXIST_ERROR);
        }
        Map<String, Object> whereMap = new HashMap<>(2);
        whereMap.put("order_code", order.getOrderCode());
        baseMapper.simpleUpdate(order, whereMap);
    }


}
